package com.pb.b2b.bean;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;

import org.apache.commons.lang.StringUtils;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.pb.b2b.model.Category;
import com.pb.b2b.model.CategoryLanguage;
import com.pb.b2b.model.Language;
import com.pb.b2b.model.enums.Status;
import com.pb.b2b.service.CategoryLanguageService;
import com.pb.b2b.service.CategoryService;
import com.pb.b2b.service.LanguageService;
import com.pb.b2b.util.FacesUtils;

@Component
@Scope("view")
@ManagedBean(name = "changeCategoryBean")
public class ChangeCategoryBean extends CategoryBaseBean {
	private static final long serialVersionUID = 1L;

	@Autowired
	LanguageService languageService;

	@Autowired
	CategoryService categoryService;

	@Autowired
	CategoryLanguageService categoryLanguageService;

	private Category selectedCategory;

	private List<Language> languageList;
	private List<CategoryLanguage> selectedCategoryLanguages;
	private boolean status;

	// --------

	// public void treeInit() {
	// Map<Long, TreeNode> treeMap = new HashedMap();
	// root = new DefaultTreeNode("Root", null);
	//
	// CategoryLanguage rootCategoryLanguage = new CategoryLanguage();
	// rootCategoryLanguage.setName("ROOT");
	// rootCategoryLanguage.setDescription("Ana Kategori");
	// Category rootCategory = new Category();
	// rootCategory.setRowNumber(0);
	// rootCategory.setStatus(true);
	// rootCategoryLanguage.setCategory(rootCategory);
	// TreeNode root2 = new DefaultTreeNode(rootCategoryLanguage, root);
	// root2.setExpanded(true);
	//
	// for (int index = 0; index < defaultCategoryLanguages.size(); index++) {
	// CategoryLanguage categoryLanguage = defaultCategoryLanguages.get(index);
	// if (categoryLanguage.getCategory().getParentCategory() == null) {
	// treeMap.put(categoryLanguage.getCategory().getId(), new
	// DefaultTreeNode(categoryLanguage, root2));
	// } else {
	// if
	// (treeMap.get(categoryLanguage.getCategory().getParentCategory().getId())
	// != null) {
	// treeMap.put(
	// categoryLanguage.getCategory().getId(),
	// new DefaultTreeNode(categoryLanguage,
	// treeMap.get(categoryLanguage.getCategory().getParentCategory().getId())));
	// treeMap.get(categoryLanguage.getCategory().getId()).setSelectable(
	// treeMap.get(categoryLanguage.getCategory().getId()).getParent().isSelectable());
	// } else {
	// defaultCategoryLanguages.add(categoryLanguage);
	// }
	// }
	// if (treeMap.get(categoryLanguage.getCategory().getId()) != null) {
	// treeMap.get(categoryLanguage.getCategory().getId()).setExpanded(true);
	// }
	// if (categoryLanguage.getId().equals(selectedCategoryLanguage.getId())) {
	// if (treeMap.get(categoryLanguage.getCategory().getId()) != null) {
	// treeMap.get(categoryLanguage.getCategory().getId()).getParent().setSelected(true);
	// treeMap.get(categoryLanguage.getCategory().getId()).setSelectable(false);
	// }
	// }
	//
	// }
	// }

	/* METHOD */

	@PostConstruct
	public void init() {
		selectedNode = (TreeNode) FacesUtils.sessionMapGet("selectedNode");
		defaultCategoryLanguages = categoryLanguageService.getDefaultCategoryLanguages();
		if (hasError()) {
			return;
		}

		languageList = languageService.getLanguages(Status.AKTIVE);
		selectedCategoryLanguage = ((CategoryLanguage) selectedNode.getData());
		selectedCategory = selectedCategoryLanguage.getCategory();
		selectedCategoryLanguages = new ArrayList<CategoryLanguage>();
		List<CategoryLanguage> categoryLanguages = categoryLanguageService.getCategoryLanguages(selectedCategory);
		for (Language language : languageList) {
			for (CategoryLanguage categoryLanguage : categoryLanguages) {
				if (categoryLanguage.getLanguage().getId().equals(language.getId())) {
					selectedCategoryLanguages.add(categoryLanguage);
				}
			}
		}
		showRootCategory = true;
		status = selectedCategory.getStatus().equals(Status.AKTIVE) ? true : false;
		treeInit();
	}

	@Override
	public String confirmAction() {
		// Language language = new Language();
		// language.setName(name);
		// language.setCode(code);
		// languageService.save(language);
		// languageList = languageService.getAllLanguages();
		if (selectedNode == null) {
			FacesUtils.addMessage(FacesMessage.SEVERITY_ERROR, "VALIDATION_KATEGORI_SECILMELI");
			return RETURN_ERROR;
		}

		for (CategoryLanguage categoryLanguage : selectedCategoryLanguages) {
			if (StringUtils.isEmpty(categoryLanguage.getName())) {
				FacesUtils.addMessage(FacesMessage.SEVERITY_ERROR, "ERROR_CATEGORY_NAME_EMPTY",
						new Object[] { categoryLanguage.getLanguage().getName() });
				return RETURN_ERROR;
			}
			if (StringUtils.isEmpty(categoryLanguage.getDescription())) {
				FacesUtils.addMessage(FacesMessage.SEVERITY_ERROR, "ERROR_CATEGORY_DESCRIPTION_EMPTY",
						new Object[] { categoryLanguage.getLanguage().getName() });
				return RETURN_ERROR;
			}
		}

		Category parentCategory = ((CategoryLanguage) selectedNode.getData()).getCategory();

		selectedCategory.setParentCategory((parentCategory != null && parentCategory.getId() != null) ? parentCategory
				: null);

		selectedCategory.setCategoryLanguages(new HashSet<CategoryLanguage>(selectedCategoryLanguages));

		selectedCategory.setStatus(status ? Status.AKTIVE : Status.PASSIVE);
		// Category parentCategory = ((CategoryLanguage)
		// selectedNode.getData()).getCategory();
		//
		// selectedCategory.setStatus(true);
		// if (parentCategory != null && parentCategory.getId() != null)
		// newCategory.setParentCategory(parentCategory);
		//
		// newCategory.setCategoryLanguages(new
		// HashSet<CategoryLanguage>(newCategoryLanguages));

		return RETURN_SUCCESS;
	}

	public String executeAction() {
		categoryService.categoryUpdate(selectedCategory);
		if (hasError()) {
			return RETURN_ERROR;
		}
		return RETURN_SUCCESS;
	}

	/* GETTER _ SETTER */
	public List<CategoryLanguage> getSelectedCategoryLanguages() {
		return selectedCategoryLanguages;
	}

	public void setSelectedCategoryLanguages(List<CategoryLanguage> selectedCategoryLanguages) {
		this.selectedCategoryLanguages = selectedCategoryLanguages;
	}

	public final CategoryLanguage getSelectedCategoryLanguage() {
		return selectedCategoryLanguage;
	}

	public final void setSelectedCategoryLanguage(CategoryLanguage selectedCategoryLanguage) {
		this.selectedCategoryLanguage = selectedCategoryLanguage;
	}

	public final boolean isStatus() {
		return status;
	}

	public final void setStatus(boolean status) {
		this.status = status;
	}

	public final Category getSelectedCategory() {
		return selectedCategory;
	}

	public final void setSelectedCategory(Category selectedCategory) {
		this.selectedCategory = selectedCategory;
	}

}
